<?php
/**
 * @version        $Id: install.php 2014年8月20日Z yeshili $
 * @package        YeshiliCMS.Admin
 * @copyright      Copyright (c) 2014 - 2050, YeshiliCMS, Inc.
 * @license        http://help.****.com/YeshiliCMS/help.html
 * @link           http://www.****.com
 */
header("Content-Type: text/html; charset=UTF-8");
error_reporting(0);
date_default_timezone_set('Asia/Shanghai');
set_time_limit(10);
error_reporting(0);
class install{
var $error;
var $message;
public function start(){
$link=@mysql_connect($_POST['host'],$_POST['user'],$_POST['pass']);
if(!$link){
$this->error='无法连接到主机';
return false;
}
if(!mysql_select_db($_POST['database'],$link)){
$this->error='无法连接到数据库';
return false;
}
$file_name = '../inc/conn.php'; //要替换操作的文件
if(!is_file($file_name)){
$this->error='文件丢失(conn.php)';
return false;
}else
{
$fp = fopen($file_name,'r'); //打开要替换的文件
if(!$fp){
	echo $file_name."文件不存在！";
}
@$conf_file = fread($fp,filesize($file_name));
$cfg_host = htmlspecialchars(stripcslashes($_POST['host']),ENT_QUOTES);
$cfg_user = htmlspecialchars(stripcslashes($_POST['user']),ENT_QUOTES);
$cfg_pass = htmlspecialchars(stripcslashes($_POST['pass']),ENT_QUOTES);
$cfg_database = htmlspecialchars(stripcslashes($_POST['database']),ENT_QUOTES);
$cfg_pre = htmlspecialchars(stripcslashes($_POST['pre']),ENT_QUOTES);
$cfg_dbcharset = htmlspecialchars(stripcslashes('utf8'),ENT_QUOTES);

$conf_file = preg_replace("/dbhost\s*\=\s(\'|\")(.*?)(\"|')/","dbhost = \"" . $cfg_host. "\"",$conf_file); 
$conf_file = preg_replace("/dbuser\s*\=\s(\'|\")(.*?)(\"|')/","dbuser = \"" . $cfg_user. "\"",$conf_file); 
$conf_file = preg_replace("/dbpass\s*\=\s(\'|\")(.*?)(\"|')/","dbpass = \"" . $cfg_pass. "\"",$conf_file);
$conf_file = preg_replace("/dbname\s*\=\s(\'|\")(.*?)(\"|')/","dbname = \"" . $cfg_database. "\"",$conf_file); 
$conf_file = preg_replace("/dbprefix\s*\=\s(\'|\")(.*?)(\"|')/","dbprefix = \"" . $cfg_pre. "\"",$conf_file);
$conf_file = preg_replace("/dbcharset\s*\=\s(\'|\")(.*?)(\"|')/","dbcharset = \"" . $cfg_dbcharset. "\"",$conf_file);
if(!@$fp = fopen($file_name,'w')) 
echo "没有写入".$file_name."的权限";
$fw = fwrite($fp, trim($conf_file));
fclose($fp);
return true;
}
}
public function dblink(){
include '../inc/conn.php';
$link=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$link)die('Could Not Connect Database! (#DB_001)');
if(!mysql_select_db($dbname,$link))die('Can\'t Use Database Please check whether there is! (#DB_002)');
mysql_query("SET time_zone = '+8:00'") or die('时区设置失败，请联系管理员！');
mysql_query("set names '".$dbcharset."'");
mysql_query("SET AUTOCOMMIT=0");
return $link;
}

public function importsql(){
$sql='show tables';
$result=mysql_query($sql,$this->dblink());
while($rows=mysql_fetch_array($result)){
if(strstr($rows[0],$_POST['pre'])){
if(!mysql_query("drop table ".$rows[0]."")){
$this->message.="删除表".$rows[0]."失败<br>";
$this->error="1";
}else{
$this->message.="删除表".$rows[0]."成功<br>";
}
}
}
$sqldata = file_get_contents('yeshili.sql');
$sqldata = str_replace('ye_',$_POST['pre'],str_replace('{bm}','utf8',$sqldata));
$explode = explode(";",$sqldata);
$explode = array_filter($explode);
$cnt = count($explode);
for ($i=0;$i<$cnt;$i++) {
$sql = $explode[$i];
$result = mysql_query($sql,$this->dblink());
if(!$result){
$this->message.="创建表失败！(".mysql_error().")<br>";
$this->error="1";
}else{
$this->message.="创建表成功！<br>";
}
}
}
}
$ret="
安装程序
<hr>
<table>
	<form action=\"\" method=\"POST\">
	<tr><td align=\"right\" width=\"100\">mysql主机</td><td><input type=\"text\" name=\"host\" value=\"localhost\"></td></tr>
	<tr><td align=\"right\">mysql帐号</td><td><input type=\"text\" name=\"user\" value=\"".$_POST['user']."\"></td></tr>
	<tr><td align=\"right\">mysql密码</td><td><input type=\"text\" name=\"pass\" value=\"".$_POST['pass']."\"></td></tr>
	<tr><td align=\"right\">数据库</td><td><input type=\"text\" name=\"database\" value=\"".$_POST['database']."\"></td></tr>
	<tr><td align=\"right\">表前缀</td><td><input type=\"text\" name=\"pre\" value=\"ye_\"> *会自动删除相同的表前缀</td></tr>
	<tr><td align=\"right\">&nbsp;</td><td><input type=\"submit\" name=\"ins\" value=\"安装\"></td></tr>
	</form>
</table>
";
$install=new install;
if($_POST){
if(!($install->start())){
$ret="<font color=\"red\">".$install->error."</font><br>".$ret."";
$val.=$ret;
}else{
$install->importsql();
if(empty($install->error)){
//@unlink('yeshili.sql');
fopen('install.ok', 'w');
$val.= $install->message."<br>己成功安装，<a href=\"/\">首页</a>";
}else{
$val.=$install->message."<Br>失败";
}
}
}else{
if(file_exists('install.ok')){
$val.='已安装成功，如想从新安装请删除文件install.ok,然后后再执行安装程序！';
}else{
if(!file_exists('yeshili.sql')){
$val.='yeshili.sql文件不存在，无法安装！';
}else{
$val.=$ret;
}
}
}
echo "
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=big5\" />
<title>安装</title>
</head>

<body>".$val."</body>
</html>
";

?>